// pages/orderList/orderList.js
import api from "../../../api/index";
import { debounce } from "../../../utils/util";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    searchParam: {
      form: "all",
    },
    //#region 列表
    list: [],
    isMore: true,
    page: 1,
    isLoading: false,
    //#endregion
    qrcodeQuery: {},
    qrCodeShow: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      list: [],
      isMore: true,
      page: 1,
      "searchParam.shop_id": wx.getStorageSync("shopID"),
    });
    this.getShopList();
    this.init();
  },

  init(e) {
    let { form } = this.data.searchParam;
    if (e && form !== "all" && form !== e) return;
    this.setData({
      list: [],
      isMore: true,
      page: 1,
    });
    this.getDataList();
  },
  getShopList() {
    api.apiStoreShopList().then((res) => {
      this.setData({
        "shopData.list": res.data.list,
        sData:
          res.data.list.find(
            (i) => i.shop_id === this.data.searchParam.shop_id
          ) || {},
      });
    });
  },
  getValue: debounce(function (e) {
    this.setData({
      "searchParam.search": e.detail.value,
    });
    this.init();
  }, 500),
  openPicker() {
    this.setData({
      show: true,
    });
  },
  getShopValue(e) {
    let v = e.detail.value;
    wx.setStorageSync("shopID", v);
    this.setData({
      "searchParam.shop_id": v,
      sData: this.data.shopData.list.find((i) => i.shop_id === v) || {},
      show: false,
    });
    this.init();
  },
  switchTab(e) {
    this.setData({
      "searchParam.form": e.detail.name,
      show: false,
    });
    this.init();
  },
  getDataList() {
    let { page, list, searchParam } = this.data;
    searchParam = {
      ...searchParam,
    };
    this.setData({
      isLoading: true,
    });
    api
      .apiStoreOrderList({
        ...searchParam,
        page,
      })
      .then((res) => {
        if (
          JSON.stringify(this.data.searchParam) !== JSON.stringify(searchParam)
        )
          return;
        let data = res.data;
        if (page >= data.all_page) this.setData({ isMore: false });
        if (data.list.length) {
          list.push(...data.list);
          this.setData({ list });
        }
      })
      .finally((_) => {
        this.setData({
          isLoading: false,
        });
      });
  },
  goAdd() {
    wx.navigateTo({
      url: "../add/index",
    });
  },
  toDetail(e) {
    wx.navigateTo({
      url: "../detail/index?id=" + e.currentTarget.dataset.id,
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (!this.data.isMore) return;
    if (this.data.isLoading) return;
    let page = this.data.page;
    this.setData({
      page: page + 1,
    });
    this.getDataList();
  },
  getQrcode(e) {
    let id = e.currentTarget.dataset.id
    Promise.all([this.getQrcodeImg(0, id), this.getQrcodeImg(1, id)]).then(res => {
       this.setData({
          qrcodeQuery: {
            show: res[0],
            download: res[1]
          },
          qrCodeShow: true,
        });
    })
  },
  getQrcodeImg(type = 0, order_id){
    return new Promise((resolve,reject) => {
      let apiUrl = type === 0 ? api
      .apiStoreOrderPayCode : api.apiStoreOrderPoster
      apiUrl({
        order_id,
      }).then(res => {
        resolve(type === 0 ? res.data : res.data.poster)
      })
    })
  },
  closeQrCode() {
    this.setData({
      qrCodeShow: false,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
